package org.campuscontent.ui.multipanel;

import java.awt.BorderLayout;

import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import org.campuscontent.config.languages.LanguageManager;
import org.campuscontent.model.MultiAllocTable;
import org.campuscontent.util.GraphInspector;

public class TableNotation extends JPanel implements AbstractUnit
{
    public static String id = "table";
    
    public int getId()
    {
        return 3;
    }

    public String id()
    {
        return id;
    }
   
    public String label()
    {
        return LanguageManager.str( AbstractUnit.class, id + ".label" );
    }

    public JComponent parent()
    {
        return this;
    }
    

    String rowsTmp = ""; //for set and exit preview
    
    public TableNotation()
    {
        alloc = new MultiAllocTable();
        allocScrollPane = new JScrollPane( alloc ); 
        setLayout( new BorderLayout() );
        add( allocScrollPane );
        
        alloc.parent = allocScrollPane;
    }


    public void initView()
    {
        // mf.rootPanel.setDividerLocation(Config.POSITION_TO_SHOW_ALLOC);
        // this.setDividerLocation(Config.POSITION_TO_HIDE_PROBLEM_CONTROLS);
        /*
        graphInspector = new GraphInspector( mf.ae.getGraph());
        if(graphInspector.validate().equals("valid"))
        {
            alloc.setAllRowToAllocTable(graphInspector.getTransitionListAsAllrows());
            alloc.getMainFrame().templateBase.allocScrollPane.validate();
        }
        */
    }

    public String getAnswer()
    {
        return "";
    }
    
    public String getParaConfig()
    {
        return alloc.allRows;
    }
    
    public void setParaConfig(String config)
    {
        /*
        alloc.setAllRowToAllocTable(config);
        alloc.getMainFrame().templateBase.allocScrollPane.validate();
        */
    }

    public boolean setPreview()
    {
        /*
        rowsTmp = alloc.getAllRows();
        if (rowsTmp.equals(""))
        {
            JOptionPane.showMessageDialog(getWindowForComponent(mf.applet), "You should set some rows!");
            return false;
        }
        if (!super.setPreview()) return false;
        mf.rootPanel.setDividerLocation(Config.POSITION_TO_SHOW_ALLOC);
        */
        return true;
    }

    public boolean exitPreview()
    {
        /*
        super.exitPreview();
        mf.rootPanel.setDividerLocation(Config.POSITION_TO_SHOW_ALLOC);
        // after set preview,user always return the alloctable he just input.
        if (!(alloc.allRows.equals("")))
        {
            alloc.deleteAllRows();
            setParaConfig(rowsTmp);
        }
        */
        return true;
    }

    public String checkAns()
    {
        /*
        graphInspector      = new GraphInspector( mf.ae.getGraph());
        String allRowsNow       = graphInspector.getTransitionListAsAllrows();
        String allRowsUser      = alloc.getAllRows();
        
        if (allRowsNow.equals(allRowsUser)) return "true";
        else
        {
            String now[] = allRowsNow.split(Config.ALLOC_TABLE_SPLITOR);
            String user[] = allRowsUser.split(Config.ALLOC_TABLE_SPLITOR);
            
            ArrayList<String> nowArray = new ArrayList<String>(java.util.Arrays.asList(now));
            ArrayList<String> userArray = new ArrayList<String>(java.util.Arrays.asList(user));
            
            if (nowArray.size()!=userArray.size()) return "false";
            else
            {
                ArrayList<String> tmpArray = nowArray;
                
                nowArray.retainAll(userArray);
                userArray.removeAll(tmpArray);
                
                if (nowArray.equals(tmpArray) && userArray.size() == 0)   //two rows equal
                    return "true";
                else
                    return "false";
            }
        }
        */
        return "";
    }
    
    public void showSolution()
    {
        /*
        super.showSolution();
        alloc.deleteAllRows();
        
        graphInspector = new GraphInspector( mf.ae.getGraph());
        alloc.setAllRowToAllocTable(graphInspector.getTransitionListAsAllrows());
        alloc.getMainFrame().templateBase.allocScrollPane.validate();
        */
    }
    
    public boolean updateToLObjectNew()
    {
//      //first check if the user have set the 
//      sAnswer = getAnswer();
//      if (sAnswer.equals("none#none#none#none#"))
//      {
//          JOptionPane.showMessageDialog(getWindowForComponent(mf.applet), "You should set the answer!");
//          return false;
//      } else
        {
            //super.updateToLObjectNew();
            return true;
        }
    }

    public void setOwner( UsersPanelContainer upc )
    {
        owner = upc;
    }
    
    public MultiAllocTable alloc;
    public JScrollPane allocScrollPane;
    
    protected GraphInspector graphInspector;
    protected UsersPanelContainer owner;
}
